﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SnailGame.Utility.DataAccess;
using System.Configuration;
using System.Collections;
using Com.Crc.PoJo.Model.Resource;

namespace Com.Crc.ResManage.DAL.EquipManage
{
    public class BroadBandManagerONUDAL
    {
        DataBroker broker = new DataBroker();

        /// <summary>
        /// 删除设备，修改状态位为0
        /// </summary>
        /// <param name="wlanList"></param>
        /// <returns></returns>
        public bool DeleteONUData(ArrayList ONUList)
        {
            string id = "";
            for (int i = 0; i < ONUList.Count; i++)
            {
                id += ONUList[i] + ",";
            }
            id = id.Substring(0, id.Length - 1);
            string strSql = @"UPDATE TT_RESOURCE_ONU SET S_STATE='0' WHERE N_ID IN (" + id + ")";
            return broker.ExecuteForOleDB(strSql, ConfigurationManager.ConnectionStrings["CRCAppConn"].ToString());
        }

        /// <summary>
        /// 修改ONU设备信息
        /// </summary>
        /// <param name="data">model实体</param>
        /// <returns></returns>
        public bool UpdateONUData(ONU data)
        {
            string strSql = @" UPDATE TT_RESOURCE_ONU
                                  SET" +
                                    "N_SUBSTATION_ID=" + data.N_SUBSTATION_ID + @",
                                        N_DEP_BRANCH_ID=" + data.N_DEP_BRANCH_ID + @",
                                        N_UNITTYPE_ID=" + data.N_UNITTYPE_ID + @",
                                        S_NAME='" + data.S_NAME + @"',
                                        S_ADDRESS='" + data.S_ADDRESS + @"',
                                        S_MAC='" + data.S_MAC + @"',
                                        S_MANGER='" + data.S_MANGER + @"',
                                        S_VLAN='" + data.S_VLAN + @"',
                                        N_OLT_ID=" + data.N_OLT_ID + @",
                                        N_PON_PORT=" + data.N_PON_PORT + @",
                                        N_S_IDX=" + data.N_S_IDX + @",
                                        N_E_IDX=" + data.N_E_IDX + @",
                                        S_BAK='" + data.S_BAK + @"',
                                        D_DATE='" + data.D_DATE.ToString("yyyy-MM-dd HH:mm:ss") + @"',
                                        S_STATE=" + data.S_STATE + @",
                                    WHERE= N_ID" +data.N_ID;
            return broker.ExecuteForOleDB(strSql, ConfigurationManager.ConnectionStrings["CRCAppConn"].ToString());
        }

        /// <summary>
        /// 新增ONU设备信息
        /// </summary>
        /// <param name="strBranchID">分局ID</param>
        /// <param name="strOfficeID">局点ID</param>
        /// <param name="strEquipID">设备ID</param>
        /// <returns></returns>
        public bool AddONUData(ONU data)
        {
            string strSql = @" INSERT INTO TT_RESOURCE_ONU(N_SUBSTATION_ID,
                                        N_DEP_BRANCH_ID,
                                        N_UNITTYPE_ID,
                                        S_NAME,
                                        S_ADDRESS,
                                        S_MAC,
                                        S_MANGER,
                                        S_VLAN,
                                        N_OLT_ID,
                                        N_PON_PORT,
                                        N_S_IDX,
                                        N_E_IDX,
                                        S_BAK,
                                        D_DATE,
                                        S_STATE)
                                    VALUES (" + data.N_SUBSTATION_ID + "," +
                                                data.N_DEP_BRANCH_ID + "," +
                                                data.N_UNITTYPE_ID + "," +
                                                "'" + data.S_NAME + "'" + "," +
                                                "'" + data.S_ADDRESS + "'" + "," +
                                                "'" + data.S_MAC + "'" + "," +
                                                "'" + data.S_MANGER + "'" + "," +
                                                "'" + data.S_VLAN + "'" + "," +
                                                data.N_OLT_ID + "," +
                                                data.N_PON_PORT + "," +
                                                data.N_S_IDX + "," +
                                                data.N_E_IDX + "," +
                                                "'" + data.S_BAK + "'" + "," +
                                                "'" + data.D_DATE.ToString("yyyy-MM-dd HH:mm:ss") + "," +
                                                "'" + data.S_STATE + "'"  +")";
            return broker.ExecuteForOleDB(strSql, ConfigurationManager.ConnectionStrings["CRCAppConn"].ToString());
        }
        /// <summary>
        /// 查询ONU设备信息
        /// </summary>
        /// <param name="strBranchID">分局ID</param>
        /// <param name="strOfficeID">局点ID</param>
        /// <param name="strEquipID">设备ID</param>
        /// <returns></returns>
        public System.Data.DataTable QueryONUData(string strBranchID, string strOfficeID, string strEquipID)
        {
            string strSql = @" SELECT N_SUBSTATION_ID,
                                        N_DEP_BRANCH_ID,
                                        N_UNITTYPE_ID,
                                        S_NAME,
                                        S_ADDRESS,
                                        S_MAC,
                                        S_MANGER,
                                        S_VLAN,
                                        N_OLT_ID,
                                        N_PON_PORT,
                                        N_S_IDX,
                                        N_E_IDX,
                                        S_BAK,
                                        D_DATE,
                                        S_STATE 
                                 FROM TT_RESOURCE_ONU 
                                WHERE S_STATE='1' ";
            if (strBranchID != "")
            {
                strSql += " AND N_DEP_BRANCH_ID = " + strBranchID;
            }
            if (strOfficeID != "")
            {
                strSql += " AND N_SUBSTATION_ID = " + strOfficeID;
            }
            if (strEquipID != "")
            {
                strSql += " AND N_ID = " + strEquipID;
            }
            return broker.ExecuteQueryForOleDB(strSql, ConfigurationManager.ConnectionStrings["CRCAppConn"].ToString());
        }
    }
}
